import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'

import BlogPostView from '../views/BlogPostView.vue';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      component:AboutView,
    },
    { 
      path: '/post/:id',
      name: 'blogPost',
      component: BlogPostView,
      // meta:{requiresAuth:true} //配合路由守卫
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 检查路由是否需要用户认证
  if (to.meta.requiresAuth && !isUserLoggedIn()) {
    // 如果用户未登录，重定向到登录页面
    next({ path: '/login' });
  } else {
    // 如果认证通过或路由不需要认证，则正常跳转
    next();
  }
});

export default router
